package com.ev.filesConverting;/**
 * @Author xy
 * @Date 2020/5/28 22:49
 * @Description
 */

import com.ev.common.domain.FtpBean;
import com.ev.common.service.FtpService;
import com.ev.common.service.impl.FtpServiceImpl;
import com.ev.filesConverting.server.FilesConvertingServer;
import com.ev.framework.config.ApplicationContextRegister;
import com.ev.framework.utils.FtpUtil;
import org.jodconverter.DocumentConverter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 文件名称： com.zd.Xthread.java</br>
 * 初始作者： xy</br>
 * 创建日期： 2020/5/28 22:49</br>
 * 功能说明： TODO <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 * ================================================<br/>
 * Copyright (c) 2020-2021 .All rights reserved.<br/>
 */
public class Office2PdfThread extends Thread{
    private String path;

    private String name;

    public Office2PdfThread(String path, String name) {
        this.path = path;
        this.name = name;
    }
    @Override
    public void run() {
        if(name.indexOf(".pdf")!=-1){
            return;
        }
        String newPath = path.substring(0,path.lastIndexOf('/'));
        String newName = path.substring(path.lastIndexOf('/')+1,path.lastIndexOf('.'));
        String suffix = path.substring(path.lastIndexOf('.')+1,path.length());
        FtpService ftpService = ApplicationContextRegister.getBean(FtpServiceImpl.class);
        InputStream in = ftpService.downFile(path,name);
        FilesConvertingServer filesConvertingserver = ApplicationContextRegister.getBean(FilesConvertingServer.class);
        OutputStream pdfFile= filesConvertingserver.convert2PDF(in,suffix);
        ByteArrayOutputStream outputStreamByte= new ByteArrayOutputStream();
        outputStreamByte=(ByteArrayOutputStream)pdfFile;
        InputStream inputStreamPdf = new ByteArrayInputStream(outputStreamByte.toByteArray());
        FtpBean ftp = FtpUtil.initFtpBean();
        ftp.setBasePath(newPath);
        ftp.setFileName(newName+".pdf");
        ftp.setInputStream(inputStreamPdf);
        FtpUtil.uploadFile(ftp);
    }
}
